# Create your views here.
from blog import access
from blog.models import Author, Blog, Setting, Category, Entry, Engine
from admin.models import EngineForm, BlogForm, CategoryForm, EntryForm, EntryCategoryForm, SetupForm
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from google.appengine.api import users

@access('public', None)
def error(request):
  blog = Blog.get()
  user = users.get_current_user()
  login_url = users.create_login_url('/admin/')
  params = {
    'blog': blog,
    'user': user,
    'login_url': login_url,
  }
  return render_to_response('admin/not_admin.html', params)

@access('admin', '/admin/error')
def setup(request):
  engine = Engine.get()
  blog = Blog.get()
  setup_form = None

  if request.method == 'POST':
    setup_form = SetupForm(engine=engine, blog=blog, data=request.POST)
    if setup_form.is_valid():
      setup_form.save();
      return HttpResponseRedirect('/admin/')
  
  params = {
    'setup_form': setup_form or SetupForm(engine=engine, blog=blog, domain=request.get_host())
  }
  
  return render_to_response('admin/setup.html', params)

@access('admin', '/admin/error')
def index(request):
  blog = Blog.get()
  if not blog: return HttpResponseRedirect('/admin/setup')
  return render_to_response('admin/admin.html', {'blog':blog})

@access('admin', '/admin/error')
def entry_create(request):
  blog = Blog.get()
  params = {
    'blog': blog,
    'title': 'Create Entry',
    'action': 'entry/create',
  }
  
  entry_form = None
  category_forms = None
  
  if request.method == 'POST':
    entry_form = EntryForm(data=request.POST)
    if entry_form.is_valid():
      entry = entry_form.save(commit=False)
      entry.blog = blog
      entry.put()
      category_forms = EntryCategoryForm.collectionFor(blog.categories, entry=entry, data=request.POST)
      filter(lambda c: c.is_valid() and c.save(), category_forms)
      return HttpResponseRedirect('/admin/')
      
  params['form'] = entry_form or EntryForm()
  params['category_forms'] = category_forms or EntryCategoryForm.collectionFor(blog.categories)
  
  return render_to_response('admin/entry/create_edit.html', params)

@access('admin', '/admin/error')
def entry_edit(request, id):
  blog = Blog.get()
  params = {
    'blog': blog,
    'title': 'Edit Entry',
    'action': 'entry/edit/%s' % id,
  }
  
  entry = Entry.get_by_id(int(id))
  entry_form = None
  category_forms = None
  
  if request.method == 'POST':
    entry_form = EntryForm(instance=entry, data=request.POST)
    if entry_form.is_valid():
      entry_form.save()
      category_forms = EntryCategoryForm.collectionFor(blog.categories, entry=entry, data=request.POST)
      filter(lambda c: c.is_valid() and c.save(), category_forms)
      return HttpResponseRedirect('/admin/')

  params['form'] = entry_form or EntryForm(instance=entry)
  params['category_forms'] = category_forms or [EntryCategoryForm(category=c, entry=entry) for c in blog.categories]
  
  return render_to_response('admin/entry/create_edit.html', params)

@access('admin', '/admin/error')
def entry_delete(request, id):
  blog = Blog.get()
  entry = Entry.get_by_id(int(id))
  
  if request.method == 'POST':
    if 'delete' in request.POST:
      entry.delete()
    return HttpResponseRedirect('/admin/entry/list')
  
  params = {
    'blog': blog,
    'entry': entry,
  }
  
  return render_to_response('admin/entry/delete.html', params)

@access('admin', '/admin/error')
def entry_list(request, page=0):
  blog = Blog.get()
  entry_list = blog.get_entries(page=page)
  params = {'blog':blog, 'entry_list':entry_list, 'page':page}
  return render_to_response('admin/entry/list.html', params)

@access('admin', '/admin/error')
def category_create(request):
  blog = Blog.get()
  params = {
    'blog': blog,
    'title': 'Create Category',
    'action': 'category/create'
  }
  
  form = None
  
  if request.method == 'POST':
    form = CategoryForm(data=request.POST)
    if form.is_valid():
      category = form.save(commit=False)
      category.blog = blog
      category.put()
      return HttpResponseRedirect('/admin/')
      
  params['form'] = form or CategoryForm()
  
  return render_to_response('admin/create_edit.html', params)
